package indzcraft.ui;

import indzcraft.loader.Reforge;

import java.io.IOException;
import java.util.List;
import java.util.Set;

import javax.swing.SwingWorker;

import net.wsnetwork.reforger.Item;
import net.wsnetwork.reforger.Reforger;
import net.wsnetwork.reforger.ReforgingOption;
import net.wsnetwork.reforger.Stat;

public class ReforgeTask extends SwingWorker<Void,Void> {
	
	IndzCraftUI icui;
	
	public ReforgeTask(IndzCraftUI ui){
		this.icui = ui;
	}
	@Override
	protected Void doInBackground() throws Exception {
		Reforger ref = null;
		try {
			ref = new Reforger();
			ref.runReforging2(icui.getCalc().getStatsEP(),icui.getLoader().getEquipment());
		} catch (InterruptedException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		List<ReforgingOption> results = ref.getResults();
		if(results != null){				
			for (ReforgingOption option : results) {
				Reforge re = new Reforge();
				for(int p = 0;p < Stat.possibleStats.length;p++){
					if(Stat.possibleStats[p].getIndex() == option.getFromStatIndex()){
						re.setFrom(Stat.possibleStats[p].getReforgeName());
					}else if(Stat.possibleStats[p].getIndex() == option.getToStatIndex()){
						re.setTo(Stat.possibleStats[p].getReforgeName());
					}
				}
				Reforge curReforge = icui.getLoader().getEquipment().get(option.getItem().getSlotName()).getReforge();
				if(!curReforge.equals(re))
					icui.getLog().append("Reforging " + option.getItem().getName() + " to -" + option.getDeltaValue() + " " + re.getFrom() + " / +" + option.getDeltaValue() + " " + re.getTo());
				//System.out.println(re.toString());
				icui.getLoader().getEquipment().get(option.getItem().getSlotName()).setReforge(re);
	        }
		}
		Set<Item> notReforgedItems = ref.getNotReforgedItems();
		for(Item item : notReforgedItems){
			icui.getLoader().getEquipment().get(item.getSlotName()).removeReforge();
			icui.getLog().append("Removed reforge from " + item.getName());
		}
		icui.showItems();
		return null;
	}
}
